package com.dxq.test.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dxq.test.common.AppManager;
import com.dxq.test.R;
import com.dxq.test.db.Label;
import com.dxq.test.db.TablesDao;

/**
 * Author: zsh
 * Date: 2023/10/18
 * Describe: 标签需求
 */
public class LabelDetailActivity extends AppCompatActivity {

    public TablesDao labelDao;

    private int id;
    private String[] types = {"牛", "羊", "禽类"};

    private ImageView ivBack;
    private TextView tvTitle, tvSerialNumber, tvName, tvWeight, tvType, tvSex, tvBreedingStatus;
    private TextView tvBatteryLevel, tvTemperature, tvExerciseSteps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_label);
        labelDao = new TablesDao(this);
        id = getIntent().getIntExtra("id", 0);
        initView();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        tvTitle = findViewById(R.id.tvTitle);
        tvSerialNumber = findViewById(R.id.tvSerialNumber);
        tvName = findViewById(R.id.tvName);
        tvWeight = findViewById(R.id.tvWeight);
        tvType = findViewById(R.id.tvType);
        tvSex = findViewById(R.id.tvSex);
        tvBreedingStatus = findViewById(R.id.tvBreedingStatus);
        tvBatteryLevel = findViewById(R.id.tvBatteryLevel);
        tvTemperature = findViewById(R.id.tvTemperature);
        tvExerciseSteps = findViewById(R.id.tvExerciseSteps);

        tvTitle.setText("标签详情");

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LabelDetailActivity.this.finish();
            }
        });
    }

    private void getData() {
        if (id == 0) {
            Toast.makeText(this, "参数异常", Toast.LENGTH_SHORT).show();
            return;
        }
        Label label = labelDao.queryLabelById(id);
        tvSerialNumber.setText(label.getSerialNumber());
        tvName.setText(label.getName());
        tvWeight.setText(label.getWeight() + "KG");
        String type = label.getType();
        int t = Integer.parseInt(type);
        tvType.setText(types[t]);
        tvSex.setText(label.getSex());
        tvBreedingStatus.setText(label.getBreedingStatus());
        tvBatteryLevel.setText(label.getBatteryLevel());
        tvTemperature.setText(label.getTemperature());
        tvExerciseSteps.setText(label.getExerciseSteps());
    }
}
